﻿Public Class Veterinaire_Screen

    Private _ctrl As Veterinaire_CTRL
    Private _flagAjout As Boolean

#Region "Cycle de vie de la fenetre veterinaire"

    Public Sub New(ByVal ctrl As Veterinaire_CTRL)

        ' Cet appel est requis par le Concepteur Windows Form.
        InitializeComponent()

        ' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
        _ctrl = ctrl

    End Sub

    Private Sub Veterinaire_Screen_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        With BarBouton1
            .PnlGestionDonnées.Visible = True
            With .BtnAjouter
                .Visible = True
                .Enabled = True
            End With
            With .BtnEnregistrer
                .Visible = True
                .Enabled = False
            End With
            With .BtnSupprimer
                .Visible = False
                .Enabled = False
            End With
            With .BtnAnnuler
                .Visible = True
                .Enabled = False
            End With
            With .BtnArchivage
                .Visible = True
                .Enabled = False
                .Location = New System.Drawing.Point(.Location.X - 80, .Location.Y)
            End With
            .PnlNavigation.Visible = True
            .BtnPremier.Enabled = False
            .BtnPrecedent.Enabled = False
        End With

        If Main_CTRL.getInstance.vetoIdentifie IsNot Nothing Then

            BTNconnection.Text = "Se deconnecter"
            BTNconnection.Tag = True
            afficherVeterinaire()
            TXTpassword.PasswordChar = ""
        Else

            BTNconnection.Text = "Se connecter"
            BTNconnection.Tag = False
            BarBouton1.PnlGestionDonnées.Enabled = False
            BarBouton1.PnlNavigation.Enabled = False
            TXTpassword.PasswordChar = "*"

        End If



    End Sub

    Private Sub Veterinaire_Screen_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed

        _ctrl.Dispose()

    End Sub

#End Region




#Region "BarBoutonVeterinaire"

    Private Sub BarBouton1_Action(ByVal sender As Object, ByVal e As ActionEventArgs) Handles BarBouton1.Action

        Select Case e.Action
            Case actionEnum.Ajouter
                afficherVeterinaire(True)
                With BarBouton1
                    .PnlNavigation.Enabled = False
                    .BtnAjouter.Enabled = False
                    .BtnAnnuler.Enabled = True
                End With
                _flagAjout = True
            Case actionEnum.Enregistrer
                If isSaisieOK Then
                    _ctrl.Enregistrer(TXTnom.Text, TXTpassword.Text)
                End If
            Case actionEnum.Annuler
                _flagAjout = False
                afficherVeterinaire()
                With BarBouton1
                    .PnlNavigation.Enabled = True
                    .BtnAjouter.Enabled = True
                    .BtnAnnuler.Enabled = False
                End With
            Case actionEnum.Archiver
                _ctrl.Archiver()
                afficherVeterinaire()
        End Select

    End Sub

    Private Sub BarBouton1_Navigation(ByVal sender As Object, ByVal e As navigationEventArgs) Handles BarBouton1.Navigation
        With BarBouton1
            Select Case e.navigation

                Case navigationEnum.Premier
                    _ctrl.indexCourant = 0
                    .BtnPrecedent.Enabled = False
                    .BtnPremier.Enabled = False
                    .BtnSuivant.Enabled = True
                    .BtnDernier.Enabled = True

                Case navigationEnum.Precedent
                    _ctrl.indexCourant -= 1
                    If _ctrl.indexCourant = 0 Then
                        .BtnPrecedent.Enabled = False
                        .BtnPremier.Enabled = False
                    End If
                    .BtnSuivant.Enabled = True
                    .BtnDernier.Enabled = True

                Case navigationEnum.Suivant
                    _ctrl.indexCourant += 1
                    If _ctrl.indexCourant = _ctrl.indiceMax Then
                        .BtnSuivant.Enabled = False
                        .BtnDernier.Enabled = False
                    End If
                    .BtnPremier.Enabled = True
                    .BtnPrecedent.Enabled = True

                Case navigationEnum.Dernier
                    _ctrl.indexCourant = _ctrl.indiceMax
                    .BtnPremier.Enabled = True
                    .BtnPrecedent.Enabled = True
                    .BtnSuivant.Enabled = False
                    .BtnDernier.Enabled = False
            End Select
        End With
        afficherVeterinaire()
    End Sub

#End Region

    ''' <summary>
    ''' Affiche le vétérinaire courant et active le bouton archivable si il l'est
    ''' </summary>
    ''' <param name="blanc">True pour afficher des champs vides</param>
    ''' <remarks></remarks>
    Private Sub afficherVeterinaire(Optional ByVal blanc As Boolean = False)
        'Dim veto As New Veterinaires
        'veto = _ctrl.vetoCourant
        If Not blanc Then
            Dim veto As Veterinaires = _ctrl.vetoCourant
            TXTcode.Text = veto.CodeVeto.ToString
            TXTnom.Text = veto.NomVeto
            TXTpassword.Text = veto.MotPasse
            BarBouton1.BtnArchivage.Enabled = _ctrl.isVetoCourantArchivable
        Else
            TXTcode.Text = "Généré Automatiquement"
            TXTnom.Text = ""
            TXTpassword.Text = ""
            BarBouton1.BtnArchivage.Enabled = False
        End If


    End Sub

    Private Function isSaisieOK() As Boolean
        ErrorProviderVeterinaire.SetError(TXTnom, "")
        ErrorProviderVeterinaire.SetError(TXTpassword, "")
        isSaisieOK = True
        If String.IsNullOrEmpty(TXTnom.Text) Then
            ErrorProviderVeterinaire.SetError(TXTnom, "Veuillez saisir un nom")
            isSaisieOK = False
        End If
        If String.IsNullOrEmpty(TXTpassword.Text) Then
            ErrorProviderVeterinaire.SetError(TXTpassword, "Veuillez saisir un mot de passe")
            isSaisieOK = False
        End If
    End Function

    Private Sub TXTnom_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TXTnom.TextChanged, TXTpassword.TextChanged
        If _flagAjout Then
            BarBouton1.BtnEnregistrer.Enabled = True
        End If
    End Sub

    Private Sub BTNconnection_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNconnection.Click

        If BTNconnection.Tag Then

            _ctrl.deconnexion()
            BTNconnection.Tag = False
            BarBouton1.PnlGestionDonnées.Enabled = False
            BarBouton1.PnlNavigation.Enabled = False
            BTNconnection.Text = "Se connecter"
            TXTcode.Text = ""
            TXTnom.Text = ""
            TXTpassword.Text = ""
            TXTpassword.PasswordChar = "*"

        Else
            If isSaisieOK() Then

                If _ctrl.sidentifier(TXTnom.Text, TXTpassword.Text) Then
                    BTNconnection.Tag = True
                    BarBouton1.PnlGestionDonnées.Enabled = True
                    BarBouton1.PnlNavigation.Enabled = True
                    BTNconnection.Text = "Se deconnecter"
                    TXTpassword.PasswordChar = ""
                    afficherVeterinaire()
                    MessageBox.Show("bienvenue sur votre compte " & Main_CTRL.getInstance.vetoIdentifie.NomVeto, "", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Else
                    MessageBox.Show("Mauvais login", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If

            End If
        End If

    End Sub
End Class